<?php

namespace App\Service\Message\v1_1_8;

// use App\Helpers\RedisUtil;

use App\Constants\ErrorCode;
use App\Helpers\RedisUtil;
use App\Model\Setting\SettingAutoMsg;
use App\Model\User;
use App\Model\UserAutoMsg;
use App\Model\UserChat;
use App\Model\UserProfile;
use App\Service\BaseService;

/**
 * 快捷消息设置
 */
class QuickMsgService extends BaseService
{
    public function handle()
    {
        $userId = $this->getUserId();
        $chatImAccount = $this->request->input('chat_im_account', 0);
        $chatUserId = User::getUserIdByIm($chatImAccount, '');
        if (!$chatUserId || $userId == $chatUserId[0]) return $this->setError(ErrorCode::PARAMETER_ERROR, '参数错误');
        $profile = UserProfile::getProfile($userId);
        $lockKey = "auto_msg_list_{$userId}_{$chatUserId[0]}";
        $myAutoList = UserAutoMsg::getTextMsgs($userId);
        // if (RedisUtil::concurrentLock($lockKey, 3600 * 240)) {
        //     $autoList = [];
        // } else {
        shuffle($autoList);
        $autoList = SettingAutoMsg::getList(SettingAutoMsg::TYPE_FAST, $profile->gender);
        // }
        $voice_list = UserAutoMsg::getUserMsgs($userId,true,'voice');
        $ret = [
            //快捷回复
            'auto_list' => $autoList,
            //自己设置的列表
            'set_list' => $myAutoList,
            //系统列表
            'sys_list' => SettingAutoMsg::getList(SettingAutoMsg::TYPE_SYS, $profile->gender),
            'voice_list' => empty($voice_list['voice']) ? [] : $voice_list['voice']
        ];
        // log_user_result($userId, $ret);
        return $ret;
    }
}
